<?php
namespace Components\User\Sha\Wallet;

class Logic extends \Components\User\Sha\Logic {
  public function pagerByfield($fields_index, $limit, $page) {
    $fieldModelName = Constants::getByIndex('user_wallet_record_fields_model_name', $fields_index);
    $fieldModelClass = __namespace__.'\\Record\\'.$fieldModelName.'Model';

    $builder = (new \Phalcon\Mvc\Model\Query\Builder())
      -> from([
        'from_user' => 'Components\User\Sha\Model',
        'user_wallet_record' => $fieldModelClass
      ])
      -> where('from_user.id = user_wallet_record.user_id')
      -> orderBy('user_wallet_record.id DESC');

    switch($fields_index) {
      case 1:
        break;
      case 2:
        break;
      case 3:
        $fieldTransferModelName = __namespace__.'\\Record\\'.$fieldModelName.'TransferModel';
        $alias = 'user_wallet_'.Constants::getByIndex('user_wallet_record_fields', $fields_index).'_record';
        $builder
					-> addFrom('Components\User\Sha\Model', 'to_user')
				  -> addFrom($fieldTransferModelName, $alias)
					-> andWhere('to_user.id = '.$alias.'.user_id AND user_wallet_record.id = '.$alias.'.id');
        break;
    }

    return $this -> getPaginate($builder, $limit, $page);
  }
}